package com.hui.demo.controller;

import com.hui.demo.error.BussinessException;
import com.hui.demo.model.User;
import com.hui.demo.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.List;

/**
 * 用户控制层
 * @author ziv
 * @date 2018-12-12
 */
@Controller
@RequestMapping(value = "user")
public class UserController {

    @Resource
    private UserService userService;

    @RequestMapping(value = "test")
    public String test (Model model) {
        // 查询数据库所有用户
        List<User> userList = userService.findAll();
        model.addAttribute("userList", userList);
        return "user";
    }

    @RequestMapping(value = "findAll")
    public String findAll(Model model) {
        List<User> userList = userService.findAll();
        model.addAttribute("userList", userList);
        throw new BussinessException("业务异常");
    }

    @RequestMapping(value = "findRetry")
    public String findRetry(Model model) {
        User user = userService.findByNameAndPasswordRetry("ziv", "123456");
        model.addAttribute(user);
        return "success";
    }
}
